package com.news;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

public class Login extends Activity {

    private EditText userName, passWord;
    private CheckBox keepLoginInfo;

    private boolean  keepLoginInfoIsChecked = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        DatabaseHandler db = new DatabaseHandler(Login.this);
        Contact userInfo = db.getContact(5);

        userName = (EditText) findViewById(R.id.userName);
        if (userInfo.getName() != "") {
            userName.setText(userInfo.getName());
        }
        passWord = (EditText) findViewById(R.id.passWord);
        if (userInfo.getPhoneNumber() != "") {
            passWord.setText(userInfo.getPhoneNumber());
        }
        keepLoginInfo = (CheckBox) findViewById(R.id.keepLoginInfo);
        if (userInfo.getPhoneNumber() != "" && userInfo.getName() != "") {
            keepLoginInfo.setChecked(true);
        }
        keepLoginInfo
                .setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked) {
                        keepLoginInfoIsChecked = isChecked;
                    }

                });

        // Reading all contacts
        Log.d("Reading: ", "Reading all contacts..");
        List<Contact> contacts = db.getAllContacts();

        for (Contact cn : contacts) {
            String log = "Id: " + cn.getID() + " ,Name: " + cn.getName()
                    + " ,Phone: " + cn.getPhoneNumber();
            // Writing Contacts to log
            Log.d("Name: ", log);
        }
    }

    public void loginSubmit(View v) {
        String user = userName.getText().toString();
        String pass = passWord.getText().toString();
        keepLoginInfoIsChecked = keepLoginInfo.isChecked();
        DatabaseHandler db = new DatabaseHandler(Login.this);
        Contact userInfo = db.getContactByName(user);
        //TODO remove below line (test code) when go live
        Log.d(userInfo.getName().toString(), userInfo.getPhoneNumber().toString());
        
        String Url = "http://test.qeedi.com/testPost.php";
        Map<String, String> p = new HashMap<String, String>();
        p.put("vid", "abc");
        p.put("k", "123");
        HttpRequest.RequestByGet(Url, p);
        
        if (user.equalsIgnoreCase("csyr") && pass.equalsIgnoreCase("123456")) {
            // modify local data
            if (keepLoginInfoIsChecked ) {
                if(user.equalsIgnoreCase(userInfo.getName().toString())) {
                    db.updateContact(new Contact(userInfo.getID(), user, pass));
                    Log.d("OPERATE", "update");
                }
                else {
                    db.addContact(new Contact(user, pass));
                }
            }
            
            Toast.makeText(this.getApplicationContext(), "success",
                    Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this.getApplicationContext(), "failed",
                    Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent i = new Intent();
            i.setClass(Login.this, MainActivity.class);
            startActivity(i);
            finish();
        }
        return super.onKeyUp(keyCode, event);
    }

}
